package com.iuie.ns.frame.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.iuie.ns.frame.annotation.SheetColumnDefine;
import com.iuie.ns.frame.free.excel.beans.SheetColumn;


/**
 * 导出Excel的辅助工具类
 * @author iuie
 * @date 2017年11月20日 下午2:59:00
 */
public class SheetColumnUtils {
	
	public static List<SheetColumn> getSheetColumns(Class<?> clazz) {
		List<SheetColumn> list = new ArrayList<>();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			SheetColumnDefine dataField = field.getAnnotation(SheetColumnDefine.class);
			if (dataField == null) {
				continue;
			}
			SheetColumn sheetColumn = new SheetColumn(field.getName(), dataField.name(), dataField.align().value(), dataField.width(), dataField.weight());
			list.add(sheetColumn);
		}
		Collections.sort(list, new Comparator<SheetColumn>() {

			@Override
			public int compare(SheetColumn sc1, SheetColumn sc2) {
				// 按照SheetColumn的weight排序
                if(sc1.getWeight() > sc2.getWeight()){  
                    return 1;  
                }  
                if(sc1.getWeight() == sc2.getWeight()){  
                    return 0;  
                }
                return -1;
			}
		});
		return list;
	}
}
